package com.ph.shopping.common.util.http;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
/**
 * 
* @ClassName: UrlRequestUtil  
* @Description: url http 请求工机具类 
* @author lijie  
* @date 2017年3月29日  
*
 */
public class UrlRequestUtil {
	
	private static final Logger log = LoggerFactory.getLogger(UrlRequestUtil.class);
	/**
	 * 
	* @Title: request   
	* @Description:根据 url 发送请求  参数接送格式请求
	* @param @param url
	* @param @param msg
	* @param @return    参数  
	* @return JSON    返回类型   json String
	* @throws
	 */
	public static String sendPostByJson(String url,JSONObject data){
		String response = null;
		DataOutputStream out = null;
		InputStream in = null;
		try {
			URL object = new URL(url);
			HttpURLConnection con = (HttpURLConnection) object.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("Accept", "application/json");
			con.setRequestMethod("POST");
			// 构造请求
			out = new DataOutputStream(con.getOutputStream());
			byte[] xmlData = data.toString().getBytes(Charset.forName("UTF-8"));
			out.write(xmlData);
			out.flush();
			
			int HttpResult = con.getResponseCode();
			if (HttpResult == HttpURLConnection.HTTP_OK) {
				in = con.getInputStream();
				byte[] bis = IOUtils.toByteArray(in);
				response = new String(bis, "UTF-8");
				if (log.isDebugEnabled()) {
					log.debug("url request call back data：" + response);
				}
			}
		} catch (Exception e) {
			log.error("url request error",e);
		}finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				log.error("close IOException error",ex);
			}
		}
		return response;
	}
	
	/**
	 * 
	* @Title: sendPost  
	* @Description: post 请求 参数格式 name=value&name=value  
	* @param @param url
	* @param @param param
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	public static String sendPost(String url, String param) {
		String result = "";
		DataOutputStream out = null;
		InputStream in = null;
		try {
			URL object = new URL(url);
			HttpURLConnection con = (HttpURLConnection) object.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);
			// 设置通用的请求属性
			con.setRequestProperty("accept", "*/*");
			con.setRequestProperty("connection", "Keep-Alive");
			con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			con.setRequestMethod("POST");
			out = new DataOutputStream(con.getOutputStream());
			byte[] xmlData = param.getBytes(Charset.forName("UTF-8"));
			out.write(xmlData);
			out.flush();

			InputStream instr = con.getInputStream();
			byte[] bis = IOUtils.toByteArray(instr);
			result = new String(bis, "UTF-8");
			if (log.isDebugEnabled()) {
				log.debug("url request call back data：" + result);
			}
		} catch (Exception e) {
			log.error("post request error",e);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				log.error("close IOException error",ex);
			}
		}
		return result;
	}
}
